//using System.Windows.Input;
	//using System.Windows.Data;
		//using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System;
using My.Resources;

//using System.Windows.Media.Imaging;
//using System.Windows.Media;
//using System.Windows.Shapes;
//using System.Windows.Controls;
//using Microsoft.Windows.Controls.Ribbon;
//using MS.WindowsAPICodePack.Internal.CoreHelpers;

	public class DocumentTab : TabItem
	{
		public DocumentTab()
		{
		    
		}
		
#region Publics
		
		public string DocName = null;
		public string FileFormat = "Rich Text Document";
		
		private RoutedEventHandler CloseEvent;
		public event RoutedEventHandler Close
		{
			add
			{
				CloseEvent = (RoutedEventHandler) System.Delegate.Combine(CloseEvent, value);
			}
			remove
			{
				CloseEvent = (RoutedEventHandler) System.Delegate.Remove(CloseEvent, value);
			}
		}
		
		public static RoutedEvent UpdateSelected =  EventManager.RegisterRoutedEvent("UpdateSelected", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));

        public static RoutedEvent UndoEvent = EventManager.RegisterRoutedEvent("Undo", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent RedoEvent = EventManager.RegisterRoutedEvent("Redo", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent CutEvent = EventManager.RegisterRoutedEvent("Cut", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent CopyEvent = EventManager.RegisterRoutedEvent("Copy", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
		public static RoutedEvent PasteEvent = EventManager.RegisterRoutedEvent("Paste", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent DeleteEvent = EventManager.RegisterRoutedEvent("Delete", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent SelectAllEvent = EventManager.RegisterRoutedEvent("SelectAll", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent FindEvent = EventManager.RegisterRoutedEvent("Find", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent ReplaceEvent = EventManager.RegisterRoutedEvent("Replace", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
        public static RoutedEvent GoToEvent = EventManager.RegisterRoutedEvent("GoTo", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(DocumentTab));
		
		public TabHeader HeaderContent = new TabHeader();
		public DocumentEditor Editor = new DocumentEditor();
		public ScrollViewer SV =new ScrollViewer();
		public Label Title =  new Label();
		public int MyIndex;
		
		public ContextMenu TBoxContextMenu = new ContextMenu();
		public MenuItem UndoMenuItem = new MenuItem();
		public MenuItem RedoMenuItem = new MenuItem();
		public MenuItem CutMenuItem = new MenuItem();
		public MenuItem CopyMenuItem = new MenuItem();
		public MenuItem PasteMenuItem = new MenuItem();
		public MenuItem DeleteMenuItem = new MenuItem();
		public MenuItem SelectAllMenuItem = new MenuItem();
		public MenuItem FindMenuItem = new MenuItem();
		public MenuItem ReplaceMenuItem = new MenuItem();
		public MenuItem GoToMenuItem = new MenuItem();
		
#endregion
		
#region FileBox
		
		public DocumentTab(string name)
		{

			this.BorderThickness = new Thickness(0, 0, 0, 0);
			DocName = name;
			HeaderContent.TabTitle.Content = name;
			HeaderContent.TabTitle.ToolTip = name;
			this.Header = HeaderContent;
			this.Height = 24;
			if (MySettings.Default.Options_SpellCheck)
			{
				SpellCheck.SetIsEnabled(Editor, true);
			}
			SV.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			SV.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
			Editor.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			Editor.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
			Editor.BorderThickness = new Thickness(0, 0, 0, 0);
			Content = Editor;
			Editor.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
			TBoxContextMenu = new ContextMenu();
		//	TBoxContextMenu.Opened += new System.Windows.RoutedEventHandler(TBoxContextMenu_Opened);
			BitmapImage undoimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\undo16.png"));
			Image undoicon = new Image();
			BitmapImage redoimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\redo16.png"));
			Image redoicon = new Image();
			BitmapImage cutimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\cut16.png"));
			Image cuticon = new Image();
			BitmapImage copyimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\copy16.png"));
			Image copyicon = new Image();
			BitmapImage pasteimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\paste16.png"));
			Image pasteicon = new Image();
			BitmapImage deleteimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\delete16.png"));
			Image deleteicon = new Image();
			BitmapImage selectallimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\selectall16.png"));
			Image selectallicon = new Image();
			BitmapImage findimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\find16.png"));
			Image findicon = new Image();
			BitmapImage replaceimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\findreplace16.png"));
			Image Replaceicon = new Image();
			BitmapImage gotoimg = new BitmapImage(new Uri(My.MyWpfExtension.Application.Info.DirectoryPath + "\\Images\\Edit\\goto16.png"));
			Image gotoicon = new Image();
			undoicon.Width = 16;
			undoicon.Height = 16;
			undoicon.Source = undoimg;
			redoicon.Width = 16;
			redoicon.Height = 16;
			redoicon.Source = redoimg;
			cuticon.Width = 16;
			cuticon.Height = 16;
			cuticon.Source = cutimg;
			copyicon.Width = 16;
			copyicon.Height = 16;
			copyicon.Source = copyimg;
			pasteicon.Width = 16;
			pasteicon.Height = 16;
			pasteicon.Source = pasteimg;
			deleteicon.Width = 16;
			deleteicon.Height = 16;
			deleteicon.Source = deleteimg;
			selectallicon.Width = 16;
			selectallicon.Height = 16;
			selectallicon.Source = selectallimg;
			findicon.Width = 16;
			findicon.Height = 16;
			findicon.Source = findimg;
			Replaceicon.Width = 16;
			Replaceicon.Height = 16;
			Replaceicon.Source = replaceimg;
			gotoicon.Width = 16;
			gotoicon.Height = 16;
			gotoicon.Source = gotoimg;
			UndoMenuItem.Header = "Undo";
			UndoMenuItem.Icon = undoicon;
			RedoMenuItem.Header = "Redo";
			RedoMenuItem.Icon = redoicon;
			CutMenuItem.Header = "Cut";
			CutMenuItem.Icon = cuticon;
			CopyMenuItem.Header = "Copy";
			CopyMenuItem.Icon = copyicon;
			PasteMenuItem.Header = "Paste";
			PasteMenuItem.Icon = pasteicon;
			DeleteMenuItem.Header = "Delete";
			DeleteMenuItem.Icon = deleteicon;
			SelectAllMenuItem.Header = "Select All";
			SelectAllMenuItem.Icon = selectallicon;
			FindMenuItem.Header = "Find";
			FindMenuItem.Icon = findicon;
			ReplaceMenuItem.Header = "Replace";
			ReplaceMenuItem.Icon = Replaceicon;
			GoToMenuItem.Header = "Go To";
			GoToMenuItem.Icon = gotoicon;
			TBoxContextMenu.Items.Add(UndoMenuItem);
			TBoxContextMenu.Items.Add(RedoMenuItem);
			TBoxContextMenu.Items.Add(new Separator());
			TBoxContextMenu.Items.Add(CutMenuItem);
			TBoxContextMenu.Items.Add(CopyMenuItem);
			TBoxContextMenu.Items.Add(PasteMenuItem);
			TBoxContextMenu.Items.Add(DeleteMenuItem);
			TBoxContextMenu.Items.Add(new Separator());
			TBoxContextMenu.Items.Add(SelectAllMenuItem);
			TBoxContextMenu.Items.Add(new Separator());
			TBoxContextMenu.Items.Add(FindMenuItem);
			TBoxContextMenu.Items.Add(ReplaceMenuItem);
			TBoxContextMenu.Items.Add(GoToMenuItem);
			Editor.ContextMenu = TBoxContextMenu;


#region add handler
            UndoMenuItem.Click+=new RoutedEventHandler(UndoMenuItem_Click);
            RedoMenuItem.Click+=new RoutedEventHandler(RedoMenuItem_Click);
            CutMenuItem.Click+=new RoutedEventHandler(CutMenuItem_Click);
            CopyMenuItem.Click+=new RoutedEventHandler(CopyMenuItem_Click);
            PasteMenuItem.Click+=new RoutedEventHandler(PasteMenuItem_Click);
            DeleteMenuItem.Click+=new RoutedEventHandler(DeleteMenuItem_Click);
            SelectAllMenuItem.Click+=new RoutedEventHandler(SelectAllMenuItem_Click);
            FindMenuItem.Click+=new RoutedEventHandler(FindMenuItem_Click);
            ReplaceMenuItem.Click+=new RoutedEventHandler(ReplaceMenuItem_Click);
            GoToMenuItem.Click+=new RoutedEventHandler(GoToMenuItem_Click);
            Editor.SelectionChanged += new RoutedEventHandler(TBox_SelectionChanged);
#endregion

        }
		
#endregion
		
#region Header
		
		public void SetFileType(string s)
		{
			HeaderContent.FileTypeImage.ToolTip = s;
			FileFormat = s;
		}
		
		public void SetDocumentTitle(string title)
		{
			HeaderContent.TabTitle.Content = title;
			DocName = title;
		}
		
		public void ShowCloseButton()
		{
			if (MySettings.Default.Options_Tabs_CloseButtonMode == 0)
			{
				HeaderContent.CloseButton.Visibility = System.Windows.Visibility.Visible;
			}
			else
			{
				if (this.IsSelected)
				{
					HeaderContent.SetMenu(true);
					HeaderContent.CloseButton.Visibility = System.Windows.Visibility.Visible;
				}
				else
				{
					HeaderContent.SetMenu(false);
					HeaderContent.CloseButton.Visibility = System.Windows.Visibility.Hidden;
				}
			}
		}
		
#endregion
		
#region TBox
		
#region Context Menu
		
		private void TBoxContextMenu_Opened(object sender, RoutedEventArgs e)
		{
			if (Editor.CanUndo)
			{
				UndoMenuItem.IsEnabled = true;
			}
			else
			{
				UndoMenuItem.IsEnabled = false;
			}
			if (Editor.CanRedo)
			{
				RedoMenuItem.IsEnabled = true;
			}
			else
			{
				RedoMenuItem.IsEnabled = false;
			}
			if ((new Microsoft.VisualBasic.Devices.Computer()).Clipboard.ContainsText())
			{
				PasteMenuItem.IsEnabled = true;
			}
			else
			{
				PasteMenuItem.IsEnabled = false;
			}
		}
		
		private void UndoMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(UndoEvent));
		}
		
		private void RedoMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(RedoEvent));
		}
		
		private void CutMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(CutEvent));
		}
		
		private void CopyMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(CopyEvent));
		}
		
		private void PasteMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(PasteEvent));
		}
		
		private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(DeleteEvent));
		}
		
		private void SelectAllMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(SelectAllEvent));
		}
		
		private void FindMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(FindEvent));
		}
		
		private void ReplaceMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(ReplaceEvent));
		}
		
		private void GoToMenuItem_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(GoToEvent));
		}
		
#endregion
		
		private void TBox_SelectionChanged(object sender, RoutedEventArgs e)
		{
			this.RaiseEvent(new RoutedEventArgs(UpdateSelected));
		}
		
#endregion
		
	}

